package com.lsh.oms.core.proxy.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.enums.RailType;
import com.lsh.oms.core.proxy.model.Position;
import com.lsh.oms.core.utils.http.HttpClientTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * Project Name: lsh-ops
 *
 * @author peter
 * @date 18/12/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Component
@Slf4j
public class FenceServiceProxy {

    private static final String RET_CODE = "ret";

    private static final String SUCCESS_CODE = "0";

    private static final String FENCE_MATH = "/fence/getFenceIds";

    @Value("${proxy.fence.url}")
    private String fenceUrl;


    public Map<String, Object> getFenceId(Position position, String areaCounty) {

        String type = RailType.FENCE_PRE.getCode().toString();
        JSONObject requestBody = new JSONObject();
        requestBody.put("type", type);
        requestBody.put("position", position);
        requestBody.put("area_county", areaCounty);

        Map<String, Object> fenceInfo;
        try {
            log.info("【获取围栏id请求url】【{}】, content:{}", fenceUrl + FENCE_MATH, requestBody.toJSONString());
            String resBody = HttpClientTemplate.doPostBody(fenceUrl + FENCE_MATH, requestBody.toJSONString());
            log.info("【获取围栏id请求返回】{}", resBody);
            if (StringUtils.isBlank(resBody)) {
                return Collections.EMPTY_MAP;
            }

            JSONObject bodyJson = JSON.parseObject(resBody);
            if (!SUCCESS_CODE.equals(bodyJson.getString(RET_CODE))) {
                return Collections.EMPTY_MAP;
            }

            JSONObject typeFenceInfo = bodyJson.getJSONObject("content");
            if (typeFenceInfo == null || typeFenceInfo.isEmpty()) {
                log.info(JSON.toJSONString(position) + "没有匹配到围栏");
                return Collections.EMPTY_MAP;
            }
            log.info("【获取围栏id请求返回 json】{}", typeFenceInfo.toJSONString());

            JSONObject levelFenceInfo = typeFenceInfo.getJSONObject(type);
            if (null == levelFenceInfo) {
                return Collections.EMPTY_MAP;
            }

            Set<String> levelKeys = levelFenceInfo.keySet();
            if (CollectionUtils.isEmpty(levelKeys) || levelKeys.size() != 1) {
                return Collections.EMPTY_MAP;
            }

            // {"msg":"success","code":0,"content":{"1":{"2":{"1477":{"fence_id":1477,"fence_name":"11"}}}}}

            String levelKey = levelKeys.iterator().next();
            JSONObject info = levelFenceInfo.getJSONObject(levelKey);
            if (null == info || info.isEmpty()) {
                return Collections.EMPTY_MAP;
            }

            Set<Map.Entry<String, Object>> entrySet = info.entrySet();
            fenceInfo = (Map<String, Object>) entrySet.iterator().next().getValue();
            log.info(position.toString() + "围栏信息  {} ", JSON.toJSONString(fenceInfo));
        } catch (Exception e) {
            log.error("获取围栏信息报错", e);
            return Collections.EMPTY_MAP;
        }

        return fenceInfo;
    }


    public static void main(String[] args) {
        String railUrl = "http://fence_server.wmdev2.lsh123.com/fence/getFenceIds";
        JSONObject position = new JSONObject();
        position.put("lng", "116.376564");
        position.put("lat", "39.962721");

        JSONObject requestBody = new JSONObject();
        requestBody.put("type", 1);
        requestBody.put("position", position);

        Map.Entry<String, Object> railEntry;
        try {
            System.out.println("【获取围栏id请求url】" + railUrl + "   " + JSON.toJSONString(requestBody));
            String content = HttpClientTemplate.doPostBody(railUrl, requestBody.toJSONString());
            System.out.println("【获取围栏id请求返回】{}" + content);

            JSONObject bodyJson = JSON.parseObject(content);
            bodyJson.getString("ret");
            if ("0".equals(bodyJson.getString("ret"))) {
                System.out.println(bodyJson.get("msg"));
            }

            JSONObject typeRailInfo = bodyJson.getJSONObject("content");

            if (typeRailInfo == null || typeRailInfo.isEmpty()) {

                System.out.println("没有匹配围栏");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
